__author__ = 'Douglas'


class DeltaRule:
    def __init__(self):
        self.w1 = 0.0
        self.w2 = 0.0
        self.w3 = 0.0
        self.rate = 0.5
        self.n_epoch = 1

    def epoch(self):
        print("***Beginning Epoch #", self.n_epoch, "***")
        self.present_pattern(0, 0, 1, 0)
        self.present_pattern(0, 1, 1, 0)
        self.present_pattern(1, 0, 1, 0)
        self.present_pattern(1, 1, 1, 1)
        self.n_epoch += 1

    @staticmethod
    def calculate_error(actual, anticipated):
        return anticipated - actual

    def present_pattern(self, i1, i2, i3, anticipated):
        print("Presented [", i1, ",", i2, ",", i3, "]")
        actual = self.recognize(i1, i2, i3)
        error = DeltaRule.calculate_error(actual, anticipated)
        print("Anticipated =", anticipated, "Actual =", actual, "Error =", error)

        # adjust weights
        self.w1 += self.training_function(i1, error)
        self.w2 += self.training_function(i2, error)
        self.w3 += self.training_function(i3, error)

    def recognize(self, i1, i2, i3):
        return ((self.w1 * i1) + (self.w2 * i2) + (self.w3 * i3)) * .5

    def run(self):
        for i in range(0, 100):
            self.epoch()

    def training_function(self, i, error):
        return self.rate * i * error